﻿using System;
using System.Data;
using System.Linq;
using System.Collections.Generic;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.Entity;
using YiYiTong.Rocket.Model;
using YiYiTong.Rocket.DataAccess;
using YiYiTong.Rocket.EFDBFirst;

namespace YiYiTong.Rocket.Business
{
    public class ShoppingCartBiz
    {
        #region 单例模式
        private ShoppingCartBiz()
        {
        }

        private static ShoppingCartBiz _instance;
        private static readonly object _syncObject = new object();

        public static ShoppingCartBiz Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_syncObject)
                    {
                        if (_instance == null)
                        {
                            _instance = new ShoppingCartBiz();
                        }
                    }
                }
                return _instance;
            }
        }
        #endregion 单例模式

        /// <summary>
        /// 商品加入购物车
        /// </summary>
        public Tuple<bool, string> Add(long uid, long goods_id)
        {
            if (uid <= 0)
                return new Tuple<bool, string>(false, "加入购物车失败，用户不存在");

            string stroutput = string.Empty;
            int ret = ShoppingCartDataAccess.Instance.Add(uid, goods_id, out stroutput);
            if (ret > 0)
                return new Tuple<bool, string>(true, "");
            else
                return new Tuple<bool, string>(false, stroutput);
        }

        /// <summary>
        /// 修改购物车商品数量
        /// </summary>
        public Tuple<bool, string> Edit(long uid, long goods_id, int num)
        {
            if (uid <= 0)
                return new Tuple<bool, string>(false, "修改商品数量失败，用户不存在");

            string stroutput = string.Empty;
            int ret = ShoppingCartDataAccess.Instance.Edit(uid, goods_id, num, out stroutput);
            if (ret > 0)
                return new Tuple<bool, string>(true, "");
            else
                return new Tuple<bool, string>(false, stroutput);
        }

        /// <summary>
        /// 删除购物车商品
        /// </summary>
        /// <param name="seq_id"></param>
        /// <returns></returns>
        public Tuple<bool, string> Del(long seq_id)
        {
            int ret = ShoppingCartDataAccess.Instance.Del(seq_id);
            if (ret > 0)
                return new Tuple<bool, string>(true, "");
            else
                return new Tuple<bool, string>(false, "删除购物车商品失败");
        }

        /// <summary>
        /// 删除所选择的商品
        /// </summary>
        public Tuple<bool, string> DelSelect(long uid, string seq_ids)
        {
            string[] seq_id = StringHelper.Split(seq_ids, ',');
            if (seq_id.Count() == 0)
                return new Tuple<bool, string>(false, "请选择商品再删除");

            //批量删除
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var shoppingcarts = dbcontext.ADZ_ShoppingCart.Where(t => t.uid == uid && seq_ids.Contains(t.seq_id.ToString())).ToList();
            if (shoppingcarts != null && shoppingcarts.Count() > 0)
            {
                foreach (var item in shoppingcarts)
                    dbcontext.ADZ_ShoppingCart.Remove(item);
                dbcontext.SaveChanges();
            }

            return new Tuple<bool, string>(true, "");
        }

        /// <summary>
        /// 获取购物车商品
        /// </summary>
        /// <returns></returns>
        public Tuple<bool, List<ShoppingGoodsModel>> GetAll(long uid)
        {
            if (uid <= 0)
                return new Tuple<bool, List<ShoppingGoodsModel>>(false, null);

            DataTable dt = ShoppingCartDataAccess.Instance.GetAll(uid);
            if (dt != null && dt.Rows.Count > 0)
            {
                List<ShoppingGoodsModel> list = new ModelHandler<ShoppingGoodsModel>().FillModel(dt, false, false);

                //ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
                //var acc = dbcontext.ADZ_MemberMaster.FirstOrDefault(t => t.uid == uid && t.is_del == 0);

                //处理图片
                foreach (var item in list)
                {
                    item.imglist = GoodsBiz.Instance.GetGoodsIMG(item.goods_id, true);

                    ////获取高于自己用户等级的商品结算价格 leev 2019-04-04
                    //if (acc != null)
                    //{
                    //    var levelconfigList = dbcontext.ADZ_MemberLevelConfig.Where(t => t.member_level > acc.member_level).ToList();
                    //    List<MemberLevelGoods> levelgoodslist = new List<MemberLevelGoods>();
                    //    foreach (var item2 in levelconfigList)
                    //    {
                    //        MemberLevelGoods levelgoods = new MemberLevelGoods();
                    //        levelgoods.member_level = item2.member_level;
                    //        levelgoods.member_name = item2.member_name;
                    //        levelgoods.level_total_money = Utils.MathRound(item.price * item.buy_amount * (1 + (decimal)item2.add_rate), 2);
                    //        levelgoodslist.Add(levelgoods);
                    //    }
                    //    item.level_goods_list = levelgoodslist;
                    //}
                }
                return new Tuple<bool, List<ShoppingGoodsModel>>(true, list);
            }
            else
                return new Tuple<bool, List<ShoppingGoodsModel>>(false, null);
        }

        /// <summary>
        /// 获取购物车单个商品
        /// </summary>
        /// <returns></returns>
        public Tuple<bool, ShoppingGoodsModel> GetInfo(long uid, long seq_id)
        {

            DataTable dt = ShoppingCartDataAccess.Instance.GetInfo(uid, seq_id);
            if (dt != null && dt.Rows.Count > 0)
            {
                var data = new ModelHandler<ShoppingGoodsModel>().FillModel(dt.Rows[0], false, false);
                data.imglist = GoodsBiz.Instance.GetGoodsIMG(data.goods_id, true);

                return new Tuple<bool, ShoppingGoodsModel>(true, data);
            }
            else
                return new Tuple<bool, ShoppingGoodsModel>(false, null);
        }
    }
}
